package com.xbongbong.paas.service.impl;

import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.CommonHelp;
import com.xbongbong.sys.help.PackageHelp;
import com.xbongbong.paas.service.HandleFieldEntity4InitService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.PackageTypeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ClueEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.enums.business.SalesOpportunityEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author: yingjf
 * @date: 2022/10/13 14:05
 * @description:
 */
@Slf4j
@Service("handleFieldEntity4InitService")
public class HandleFieldEntity4InitServiceImpl implements HandleFieldEntity4InitService {
    @Resource
    private CommonHelp commonHelp;
    @Resource
    private PackageHelp packageHelp;
    @Override
    public void handleFieldEntity4Init(String corpid, XbbRefTypeEnum xbbRefTypeEnum, FieldAttrEntity fieldAttrEntity) throws XbbException {
        try {
            if (Objects.isNull(corpid) || Objects.isNull(xbbRefTypeEnum) || Objects.isNull(fieldAttrEntity)) {
                return;
            }

            Integer businessType = xbbRefTypeEnum.getCode();

            if (Objects.equals(businessType, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode())) {
                if (Objects.equals(fieldAttrEntity.getAttr(), CustomerManagementEnum.PUBLIC_GROUP.getAttr())) {
                    if (commonHelp.isCustomerPublicPoolSeniorModel(corpid)) {
                        fieldAttrEntity.setIsOpen(BasicConstant.ONE);
                    }
                }

            } else if (Objects.equals(businessType, XbbRefTypeEnum.CLUE.getCode())) {
                if (Objects.equals(fieldAttrEntity.getAttr(), ClueEnum.PUBLIC_GROUP.getAttr())) {
                    if (commonHelp.isCluePublicPoolSeniorModel(corpid)) {
                        fieldAttrEntity.setIsOpen(BasicConstant.ONE);
                    }
                }
            } else if(Objects.equals(businessType, XbbRefTypeEnum.SALES_OPPORTUNITY.getCode())){
                if (Objects.equals(fieldAttrEntity.getAttr(), SalesOpportunityEnum.LINK_COMPETITOR.getAttr())
                    ||Objects.equals(fieldAttrEntity.getAttr(), SalesOpportunityEnum.WIN_COMPETITOR.getAttr())) {
                    if (Objects.equals(packageHelp.getFeeType(corpid), PackageTypeEnum.ULTIMATE.getType())) {
                        fieldAttrEntity.setIsOpen(BasicConstant.ONE);
                    }else{
                        fieldAttrEntity.setIsOpen(BasicConstant.ZERO);
                    }
                }
            }
        }catch (Exception e){
            log.error("com.xbongbong.paas.service.impl.HandleFieldEntity4InitServiceImpl.handleFieldEntity4Init",e);
        }
    }
}
